package com.express.util;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

public class DateUtils {
  public static final int day = 86400000;
  
  public static SimpleDateFormat matter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  public static SimpleDateFormat matter5 = new SimpleDateFormat("yyyy-MM-dd HH:mm");

  public static SimpleDateFormat matter2 = new SimpleDateFormat("yyyy-MM-dd");
  
  public static SimpleDateFormat matterch = new SimpleDateFormat("yyyy");
  
  public static SimpleDateFormat matter3 = new SimpleDateFormat("yyyyMMdd");

  public static SimpleDateFormat matter4 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");

  public static String getEarly() {
    Calendar calendar = Calendar.getInstance();
    calendar.set(5, 1);
    return matter2.format(calendar.getTime());
  }
  
  public static Date addByYear(Date date, int years) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.add(1, years);
    calendar.add(13, -1);
    return calendar.getTime();
  }
  
  public static Date addDay(Date date, int day) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.add(6, day);
    return calendar.getTime();
  }
  
  public static Date addDateMinut(Date date, int hour) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.add(10, hour);
    return calendar.getTime();
  }
  
  public static Date subOneYear(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.add(1, -1);
    return calendar.getTime();
  }
  
  public static String longToStr(long l) {
    try {
      Date dt = new Date(l);
      String sDateTime = matter1.format(dt);
      return sDateTime;
    } catch (Exception e) {
      return "";
    } 
  }
  
  public static String longToStrCh(long l) {
    try {
      Date dt = new Date(l);
      String sDateTime = matterch.format(dt);
      return sDateTime;
    } catch (Exception e) {
      return "";
    } 
  }
  
  public static long strToLong13(String s) {
    try {
      Date date = matter1.parse(s);
      return date.getTime();
    } catch (Exception e) {
      return 0L;
    } 
  }
  
  public static long strToLong10(String s) {
    try {
      Date date = matter1.parse(s);
      return date.getTime() / 1000L;
    } catch (Exception e) {
      return 0L;
    } 
  }
  
  public static long getLongTime() {
    return (new Date()).getTime();
  }
  
  public static String getToday() {
    try {
      Date as = new Date(System.currentTimeMillis());
      return matter2.format(as) + " 00:00:00";
    } catch (Exception e) {
      e.printStackTrace();
      return "";
    } 
  }
  
  public static String getYesterday() {
    try {
      Date as = new Date((new Date()).getTime() - 86400000L);
      return matter2.format(as) + " 00:00:00";
    } catch (Exception e) {
      e.printStackTrace();
      return "";
    } 
  }
  
  public static Date lastThreeMonth(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.add(2, -3);
    return calendar.getTime();
  }
  
  public static String getThreeMonthAgo() {
    try {
      Date as = lastThreeMonth(new Date());
      return matter2.format(as) + " 00:00:00";
    } catch (Exception e) {
      e.printStackTrace();
      return "";
    } 
  }
  
  public static Long getLastMonthTime() {
    Calendar calendar = Calendar.getInstance();
    Date date = new Date();
    calendar.setTime(date);
    calendar.add(2, -1);
    return Long.valueOf(strToLong13(matter1.format(calendar.getTime())));
  }
  
  public static Long getCurrentTime() {
    Calendar calendar = Calendar.getInstance();
    Date date = new Date();
    calendar.setTime(date);
    return Long.valueOf(strToLong13(matter1.format(calendar.getTime())));
  }
  
  public static String format(Date date, String pattern) {
    SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    String dateString = sdf.format(date);
    return dateString;
  }
  
  public static int dayForWeek(Date date) throws Exception {
    Calendar c = Calendar.getInstance();
    c.setTime(date);
    int dayForWeek = 0;
    if (c.get(7) == 1) {
      dayForWeek = 7;
    } else {
      dayForWeek = c.get(7) - 1;
    } 
    return dayForWeek;
  }
  
  public static int differentDays(Date date1, Date date2) {
    Calendar cal1 = Calendar.getInstance();
    cal1.setTime(date1);
    Calendar cal2 = Calendar.getInstance();
    cal2.setTime(date2);
    int day1 = cal1.get(6);
    int day2 = cal2.get(6);
    int year1 = cal1.get(1);
    int year2 = cal2.get(1);
    if (year1 != year2) {
      int timeDistance = 0;
      for (int i = year1; i < year2; i++) {
        if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0) {
          timeDistance += 366;
        } else {
          timeDistance += 365;
        } 
      } 
      return timeDistance + day2 - day1;
    } 
    return day2 - day1;
  }
  
  public static int differentMonths(Date date1, Date date2) {
    Calendar bef = Calendar.getInstance();
    Calendar aft = Calendar.getInstance();
    bef.setTime(date1);
    aft.setTime(date2);
    int result = aft.get(2) - bef.get(2);
    int month = (aft.get(1) - bef.get(1)) * 12;
    return Math.abs(month + result);
  }
  
  public static int getDate(Date date, int type) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    return cal.get(type);
  }
  
  public static String getRandomChar(int length) {
    char[] chr = { 
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 
        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 
        'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 
        'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 
        'E', 'F', 'G', 'H', 'I', 'J', 'K', 'l', 'M', 'N', 
        'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 
        'Y', 'Z' };
    Random r = new Random();
    StringBuffer buf = new StringBuffer();
    for (int i = 0; i < length; i++)
      buf.append(chr[r.nextInt(62)]); 
    return buf.toString();
  }
  
  public static void main(String[] args) throws Exception {
    System.out.println(DateUtil.parse("2025-04-21 15:50:00", DatePattern.NORM_DATETIME_PATTERN));
    System.out.println(DateUtils.strToLong13("2025-04-21 15:50:00"));
  }
}
